package com.ruoyi.project.emr.service.impl;

import java.util.List;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.project.emr.domain.EmrRecordInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.emr.mapper.EmrMedicalRecordMapper;
import com.ruoyi.project.emr.domain.EmrMedicalRecord;
import com.ruoyi.project.emr.service.IEmrMedicalRecordService;

/**
 * 病历Service业务层处理
 *
 * @author gzu
 * @date 2023-07-22
 */
@Service
public class EmrMedicalRecordServiceImpl implements IEmrMedicalRecordService
{
    @Autowired
    private EmrMedicalRecordMapper emrMedicalRecordMapper;

    /**
     * 查询病历相关任务信息
     * @param recordInfo
     * @return
     */
    @Override
    public List<EmrRecordInfo> selectRecordInfo(EmrRecordInfo recordInfo) {
        return emrMedicalRecordMapper.selectEmrRecordInfo(recordInfo);
    }

    /**
     * 查询病历
     *
     * @param id 病历主键
     * @return 病历
     */
    @Override
    public EmrMedicalRecord selectEmrMedicalRecordById(String id) {
        return emrMedicalRecordMapper.selectEmrMedicalRecordById(id);
    }

    /**
     * 查询病历列表
     *
     * @param emrMedicalRecord 病历
     * @return 病历
     */
    @Override
    public List<EmrMedicalRecord> selectEmrMedicalRecordList(EmrMedicalRecord emrMedicalRecord)
    {
        return emrMedicalRecordMapper.selectEmrMedicalRecordList(emrMedicalRecord);
    }

    /**
     * 新增病历
     *
     * @param medicalRecord 病历信息
     * @return 结果
     */
    @Override
    public int insertEmrMedicalRecord(EmrMedicalRecord medicalRecord)
    {
        medicalRecord.setCreateTime(DateUtils.getNowDate());
        medicalRecord.setDate(DateUtils.getNowDate());

        while(true) {
            //生成病历号
            String prefix = "EMR";
            Random random = new Random();
            // 生成一个7位数的随机整数
            Integer randomNumber = random.nextInt(9000000) + 1000000;
            medicalRecord.setNumber(prefix+String.valueOf(randomNumber));
            List<EmrMedicalRecord> exists = emrMedicalRecordMapper.selectEmrMedicalRecordByNumber(medicalRecord.getNumber());
            System.err.println(exists.size());
            if(exists.size() == 0) {
                break;
            }
        }

        return emrMedicalRecordMapper.insertEmrMedicalRecord(medicalRecord);
    }

    /**
     * 修改病历
     *
     * @param emrMedicalRecord 病历
     * @return 结果
     */
    @Override
    public int updateEmrMedicalRecord(EmrMedicalRecord emrMedicalRecord)
    {
        emrMedicalRecord.setUpdateTime(DateUtils.getNowDate());
        return emrMedicalRecordMapper.updateEmrMedicalRecord(emrMedicalRecord);
    }

    /**
     * 批量删除病历
     *
     * @param ids 需要删除的病历主键
     * @return 结果
     */
    @Override
    public int deleteEmrMedicalRecordByIds(String[] ids)
    {
        return emrMedicalRecordMapper.deleteEmrMedicalRecordByIds(ids);
    }

    /**
     * 删除病历信息
     *
     * @param id 病历主键
     * @return 结果
     */
    @Override
    public int deleteEmrMedicalRecordById(String id)
    {
        return emrMedicalRecordMapper.deleteEmrMedicalRecordById(id);
    }

    /**
     * 查询病历关联病人列表
     *
     * @param id
     * @return
     */
    @Override
    public List<EmrMedicalRecord> selectEmrMedicalRecordListByPatientId(String id) {
        return emrMedicalRecordMapper.selectEmrMedicalRecordListByPatientId(id);
    }
}
